<template>
  <div class="login_bg">
    <div class="login_img">
      <img class="login_logo" src="@/assets/login/login_logo.png" alt="">
      <img class="login_icon" src="@/assets/login/login_icon.png" alt="">
    </div>

    <div class="login">
      <p class="title">{{ identifyList[index] }}端-找回密码</p>

      <!-- 输入表单组件 status-icon显示校验反馈图标 rules绑定校验规则 :label-position表单对齐方向 -->
      <el-form :model="updateUser" status-icon :rules="rules" :label-position="'left'" ref="updateUser"
        label-width="100px">
        <el-form-item label="账号" prop="id">
          <el-input type="text" v-model="updateUser.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input type="text" v-model="updateUser.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input type="text" v-model="updateUser.password" autocomplete="off" show-password />
        </el-form-item>
        <el-form-item class="msgCode" label="验证码" prop="msgCode">
          <el-input type="text" v-model="updateUser.msgCode" autocomplete="off" />
          <el-link type="primary" :underline="false" @click="getCode">
            {{ second === totalSecond ? '获取短信验证码' : second + '秒后重新发送' }}
          </el-link>
        </el-form-item>
      </el-form>

      <el-link class="login_identify" :underline="false" @click="changeList">我是{{ identifyList[index] }}</el-link>
      <el-link class="back" type="danger" :underline="false" @click="$router.go(-1)">返回</el-link>
      <el-button class="login_bt" type="primary" plain @click="submitForm('updateUser')">修改密码</el-button>
    </div>

    <footer>Copyright © 2023.广应科你这瓜保熟吗</footer>
  </div>
</template>

<script>
import { updatePassword } from '@/api/login'

export default {
  name: 'FindPasswordPage', // 找回密码页面
  data () {
    return {
      totalSecond: 60, // 总秒数
      second: 60, // 当前秒数
      timer: null, // 定时器 id
      identifyList: ['教师', '教务', '学生'], // 存放身份的数组
      index: 0, // 用于标识identifyList的下标
      // 存放修改密码的教师和学生信息
      updateUser: {
        id: '',
        password: '',
        phone: '',
        msgCode: '' // 短信验证码
      },
      // 设置对应的校验规则
      rules: {
        id: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^\d{12}$/, message: '账号必须为12位数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '密码必须为6位数字', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式错误',
            trigger: 'blur'
          }
        ],
        msgCode: [
          { required: true, message: '请输入短信验证码', trigger: 'blur' },
          { pattern: 123456, message: '短信验证码错误', trigger: 'blur' } // 短信验证码写死为123456
        ]
      }
    }
  },
  methods: {
    // 切换身份
    changeList () {
      this.index++
      if (this.index >= this.identifyList.length) this.index = 0
    },
    // 获取短信验证码
    getCode () {
      // 当前目前没有定时器开着 且 totalSecond 和 second 一致 (秒数归位) 才可以倒计时
      if (!this.timer && this.second === this.totalSecond) {
        // 开启倒计时
        this.timer = setInterval(() => {
          this.second--

          if (this.second <= 0) {
            clearInterval(this.timer)
            this.timer = null // 重置定时器id
            this.second = this.totalSecond // 归为
          }
        }, 1000)
      }
    },
    // 提交表单 发送请求 修改密码
    submitForm (updateUser) {
      // 登录前校验数据是否合法
      this.$refs[updateUser].validate(async (valid) => {
        if (valid) {
          await updatePassword(this.updateUser, this.identifyList[this.index])
          this.$message.success('密码修改成功')
          this.$router.go(-1) // 返回登录页
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login_bg {
  position: relative;
  width: 100%;
  height: 100%;
  background: url('/src/assets/login/login_bg.jpg') no-repeat;
  background-size: 100% 100%;
  background-position: center 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1;

  .login_img {
    width: 30%;
    z-index: 3;

    .login_logo {
      display: block;
      width: 100%;
      height: auto;
    }

    .login_icon {
      margin-top: 10%;
      margin-left: 10%;
      display: block;
      width: 100%;
      height: auto;
    }
  }

  .login {
    width: 375px;
    height: auto;
    margin-left: 10%;
    padding: 2%;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);

    .title {
      margin: 0px auto 20px auto;
      text-align: left;
      font-size: 30px;
      font-weight: bolder;
      color: #3c3c3c;
    }

    .msgCode {
      margin-bottom: 0px;
    }

    .login_identify {
      float: right;
      font-size: medium;
      font-weight: bold;
      margin: 10px 15px;
    }

    .back {
      float: left;
      font-size: medium;
      font-weight: bold;
      margin: 10px 15px;
    }

    .login_bt {
      width: 100%;
      margin-top: 10px;
      float: right;
    }
  }

  footer {
    position: absolute;
    bottom: 4%;
    width: 100%;
    text-align: center;
  }
}
</style>
